<template>
<!--  添加评论与修改页面-->
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? '新增' : '编辑'" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
             label-width="120px">
<!--      <el-form-item prop="isHide" label="是否显示">-->
<!--        <ren-select v-model="dataForm.isHide" dict-type="is_hide" placeholder="请选择是否显示评论"></ren-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item prop="serviceScore" label="商品评分">-->
<!--        <el-input-number v-model="dataForm.productScore" placeholder="商品的评分数值表示，如1-5" :precision="0" :min="1" :max="5"></el-input-number>-->
<!--      </el-form-item>-->
<!--      <el-form-item prop="productScore" label="服务评分">-->
<!--        <el-input-number v-model="dataForm.serviceScore" placeholder="请输入服务评分数值表示，如1-5" :precision="0" :min="1" :max="5"></el-input-number>-->
<!--      </el-form-item>-->
      <el-form-item prop="reply" label="回复">
        <el-input v-model="dataForm.reply" placeholder="请输入回复评论内容"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'

export default {
  data() {
    return {
      visible: false,
      productList: [],//商品列表
      dataForm: {
        id: '',
        content: '',                    //用户对商品的评价和留言
        productScore: '',               //用户对商品的评分，通常使用数值表示，如1-5
        serviceScore: '',               //服务评分1-5分
        reply: '',                      //商家回复
        isHide: '',	                    //用于标识是否隐藏评论1隐藏0不隐藏
        productId: '',                  //关联到所属商品的ID
        specsId: '',	                  //关联到所评论的规格ID
        userId: '',                     //关联到发表评论的用户的ID
      }
    }
  },
  computed: {
    dataRule() {
      return {
        reply: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/api/productComment/${this.dataForm.id}`).then(({data: res}) => {
        //console.log(res)
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        let item = {
          id: this.dataForm.id,
          reply: this.dataForm.reply,
          isHide: this.dataForm.isHide}
        this.$http[!this.dataForm.id ? 'post' : 'put']('/api/productComment',this.dataForm.id?item : this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {
        })
      })
    }, 1000, {'leading': true, 'trailing': false}),
  }
}
</script>
